package org.bsplus.util;

import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.apache.tools.ant.Project;
import org.dbunit.ant.DbUnitTask;
import org.dbunit.ant.Operation;
import org.dbunit.database.DatabaseDataSourceConnection;
import org.dbunit.database.IDatabaseConnection;
import org.springframework.beans.factory.annotation.Required;

public class DBUnitBootstrapper extends DbUnitTask
{

    private DataSource dataSource;

    public DBUnitBootstrapper()
    {
        /*
         this is required to prevent a null pointer exception when executing
         ant tasks otside of ant
         https://issues.apache.org/bugzilla/show_bug.cgi?id=38458
         */
        setProject(new Project());
    }

    @Resource
    @Required
    public void setDataSource(DataSource dataSource)
    {
        this.dataSource = dataSource;
    }

    public void setOperations(List<Operation> operations)
    {
        for(Operation operation: operations)
        {
            addOperation(operation);
        }
    }

    @Override
    protected IDatabaseConnection createConnection() throws SQLException
    {
        return new DatabaseDataSourceConnection(dataSource);
    }

}
